/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Travelagent;
import edu.chl.da.ejb.ctrl.IManageAgent;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;


/**
 * Controller bean managing listing, getting and removal events of travelagents
 * @author Erik
 */
@ManagedBean(name = "ManageTravelagentsBean")
@SessionScoped
public class ManageTravelagentsBean {

    private List<Travelagent> travelagents;
    private String selectedTravelagentUsername;

    private boolean success = false;
    private String errorMsg;

    @EJB
    IManageAgent ima;

    /** Creates a new instance of ManageCustomersBean */
    public ManageTravelagentsBean() {
        
    }

    public void loadTravelagents() {
        this.travelagents = ima.getTravelagentList();
        
    }
    
    public String deleteTravelagent() {
        Travelagent a = ima.getTravelagent(selectedTravelagentUsername);
        boolean b = ima.removeTravelagent(a);
        if(b) {
            travelagents.remove(a);
            return "/admin/manageTravelagents.jsf";
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Gick ej att radera agenten, kontrollera eventuella fel"));
        return null;
    }

    public String navigate() {
        if (success) {
            return "/admin/manageTravelagents.jsf";
        } else {
            // Adding a failure message. Possible to display all
            // messages using <h:messages> (ending s!!)
            // or just target a specific control using <h:message> (no s!).
            FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(errorMsg));
           return null; //Same page;

        }
    }

    public List<Travelagent> getTravelagents() {
        return travelagents;
    }

    public void setTravelagents(List<Travelagent> travelagents) {
        this.travelagents = travelagents;
    }

    public String getSelectedTravelagentUsername() {
        return selectedTravelagentUsername;
    }

    public void setSelectedTravelagentUsername(String selectedTravelagentUsername) {
        this.selectedTravelagentUsername = selectedTravelagentUsername;
    }
}
